package com.opensky.qatest.ui.page;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.common.Properties;
import com.opensky.qatest.util.junit.Assert;
import com.opensky.qatest.util.selenium.HTML;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Curators piccker page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.1
 * 
 */
public class CuratorPickerPage extends LoadableComponent<CuratorPickerPage> implements IPage {

    // ************************ Locators ************************

    private static final String CURATOR_PICKER_PAGE_URL = Properties.get("env.root.url") + "curator-picker";

    private static final String PERSONALIZE_STEP = "//header//li[@class='active']/span[text()='Personalize']";

    private static final String CONTINUE_BTN = "//div[@class='footer-wrap']//input[@value='Continue']";

    private static final String RECOMMENDED_CATEGORY_LINK = "Recommended";

    private static final String FOOD_CATEGORY_LINK = "Food";

    private static final String HEALTH_CATEGORY_LINK = "Health";

    private static final String HOME_CATEGORY_LINK = "Home";

    private static final String STYLE_CATEGORY_LINK = "Style";

    private static final String EMAILS_CATEGORY_LINK = "OpenSky Emails";

    private static final String YOUR_CURATORS_TITLE = "//div[@class='aside-block following-list']/h3[text()='Your Curators']";

    private static final String SLOT_LIST_ITEM = "//div[@class='aside-block following-list']/ul[@class='slots curators']/li";

    private static final String EMPTY_SLOT = "//div[@class='aside-block following-list']/ul[@class='slots curators']/li[@class='curator empty-slot']";

    private static final String CURATOR_SLOT = "//div[@class='aside-block following-list']/ul[@class='slots curators']/li[@class='curator']";

    private static final String CURATOR_SLOT_UNFOLLOW_BTN = "//div[@class='aside-block following-list']/ul[@class='slots curators']/li[@class='curator']//input[@name='unfollow']";

    private static final String CURATOR_LIST = "//div[@class='seller-picker']//ul";

    private static final String CURATOR_BOX = "//div[@class='seller-picker']//ul/li[1]";

    private static final String DEFAULT_PICKER = "//div[@class='seller-picker']/div[1]/div[@class='category-header' and contains(., 'Recommendations from OpenSky')]";

    private static final String CURATOR_AVATAR = "//div[@class='seller-picker']//ul/li[1]/div[@class='avatar']/img";

    private static final String CURATOR_NAME = "//div[@class='seller-picker']//ul/li[1]//div[@class='bio']//h3";

    private static final String CURATOR_PERSONAL_TAG = "//div[@class='seller-picker']//ul/li[1]//div[@class='bio']//h4";

    private static final String CURATOR_RECENT_SALE_LIST = "//div[@class='seller-picker']//ul/li[1]//div[@class='offers']//div[@class='recent']";

    private static final String CURATOR_RECENT_SALE = "//div[@class='seller-picker']//ul/li[1]//div[@class='offers']//div[@class='recent']//ul/li/a";

    private static final String FOLLOW_BTN = "//div[@class='seller-picker']//ul/li[1]//a[text()='Follow']";

    private static final String UNFOLLOW_BTN = "//div[@class='seller-picker']//ul/li[1]//input[@value='unfollow']";

    private static final String FOLLOWERS = "//div[@class='seller-picker']//ul/li[1]//span[@class='followers']";

    private static final String FOLLOWING_MESSAGE_CONTAINER = "//div[@class='seller-picker']//ul/li[1]//div[@class='is-following']";

    private static final String FOLLOWING_MESSAGE_TEXT = "You are following";

    // ************************ Complex page objects **************************

    private final WebDriverWrapper driver;

    public CuratorPickerPage(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(CURATOR_PICKER_PAGE_URL);
    }

    public OfferPage openCuratorRecentSale() {

	WebElement productLink = driver.waitAssertAndGetElement("CURATOR_RECENT_SALE", By.xpath(CURATOR_RECENT_SALE));

	String href = productLink.getAttribute(HTML.HREF);
	String curator = href.split("/")[3];
	String product = href.split("/")[5];

	OfferPage offerPage = new OfferPage(driver, curator, product);

	driver.get(href);

	return offerPage.isPageLoaded() ? offerPage : null;
    }

    public void clickFollowCuratorBtn() {
	driver.waitAndSendEnter("FOLLOW_BTN", By.xpath(FOLLOW_BTN));
    }

    public void clickUnfollowCuratorBtn() {
	driver.waitAndSendEnter("UNFOLLOW_BTN", By.xpath(UNFOLLOW_BTN));
    }

    public void clickUnfollowCuratorBtnFromLeftNav() {
	driver.waitAndSendEnter("CURATOR_SLOT_UNFOLLOW_BTN", By.xpath(CURATOR_SLOT_UNFOLLOW_BTN));
    }

    public EmailsPage openOpenskyEmails() {

	EmailsPage emailsPage = new EmailsPage(driver);
	driver.waitAndSendEnter("EMAILS_CATEGORY_LINK", By.linkText(EMAILS_CATEGORY_LINK));
	return emailsPage.isPageLoaded() ? emailsPage : null;
    }

    public InvitePage clickContinueBtn() {

	InvitePage invitePage = new InvitePage(driver);

	driver.waitAndSendEnter("CONTINUE_BTN", By.xpath(CONTINUE_BTN));

	if (invitePage.isPageLoaded()) {
	    return invitePage;
	} else {
	    return null;
	}
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() {
	Assert.assertUrlEqual(CURATOR_PICKER_PAGE_URL, driver.getCurrentUrl());
    }

    @Override
    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(CURATOR_PICKER_PAGE_URL);
    }

    public boolean isBaseElementsPresent() {

	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("PERSONALIZE_STEP", By.xpath(PERSONALIZE_STEP)),
		driver.waitAndVerifyElementPresent("CONTINUE_BTN", By.xpath(CONTINUE_BTN)),
		driver.waitAndVerifyElementPresent("RECOMMENDED_CATEGORY_LINK", By.linkText(RECOMMENDED_CATEGORY_LINK)),
		driver.waitAndVerifyElementPresent("FOOD_CATEGORY_LINK", By.linkText(FOOD_CATEGORY_LINK)),
		driver.waitAndVerifyElementPresent("HEALTH_CATEGORY_LINK", By.linkText(HEALTH_CATEGORY_LINK)),
		driver.waitAndVerifyElementPresent("HOME_CATEGORY_LINK", By.linkText(HOME_CATEGORY_LINK)),
		driver.waitAndVerifyElementPresent("STYLE_CATEGORY_LINK", By.linkText(STYLE_CATEGORY_LINK)),
		driver.waitAndVerifyElementPresent("EMAILS_CATEGORY_LINK", By.linkText(EMAILS_CATEGORY_LINK)),
		driver.waitAndVerifyElementPresent("YOUR_CURATORS_TITLE", By.xpath(YOUR_CURATORS_TITLE)),
		driver.waitAndVerifyElementPresent("EMPTY_SLOT", By.xpath(EMPTY_SLOT)));
    }

    public boolean isCuratorsPickerListBaseElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("CURATOR_LIST", By.xpath(CURATOR_LIST)),
		driver.waitAndVerifyElementPresent("DEFAULT_PICKER", By.xpath(DEFAULT_PICKER)));
    }

    public boolean isCuratorBaseElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("CURATOR_LIST", By.xpath(CURATOR_LIST)),
		driver.waitAndVerifyElementPresent("CURATOR_AVATAR", By.xpath(CURATOR_AVATAR)),
		driver.waitAndVerifyElementPresent("CURATOR_NAME", By.xpath(CURATOR_NAME)),
		driver.waitAndVerifyElementPresent("CURATOR_PERSONAL_TAG", By.xpath(CURATOR_PERSONAL_TAG)),
		driver.waitAndVerifyElementPresent("CURATOR_RECENT_SALE_LIST", By.xpath(CURATOR_RECENT_SALE_LIST)),
		driver.waitAndVerifyElementPresent("FOLLOW_BTN", By.xpath(FOLLOW_BTN)),
		driver.waitAndVerifyElementPresent("FOLLOWERS", By.xpath(FOLLOWERS)));
    }

    public boolean isFollowCuratorReactCorrect() {

	driver.waitAndVerifyElementPresent("UNFOLLOW_BTN", By.xpath(UNFOLLOW_BTN));
	driver.waitAndVerifyElementWithTextPresent("FOLLOWING_MESSAGE_CONTAINER", By.xpath(FOLLOWING_MESSAGE_CONTAINER),
		FOLLOWING_MESSAGE_TEXT);

	WebElement curatorBox = driver.waitAssertAndGetElement("CURATOR_BOX", By.xpath(CURATOR_BOX));

	if (!curatorBox.getAttribute(HTML.CLASS).contains("following")) {
	    return false;
	}

	List<WebElement> slots = driver.waitAssertAndGetElements("SLOT_LIST_ITEM", By.xpath(SLOT_LIST_ITEM));

	String curatorInList = driver.waitAssertAndGetElement("CURATOR_BOX", By.xpath(CURATOR_BOX)).getAttribute("data-curator-slug");
	String curatorInSlot = slots.get(0).getAttribute("data-curator-slug");

	if (!curatorInList.equals(curatorInSlot)) {
	    return false;
	}

	return true;
    }

    public boolean isUnfollowCuratorReactCorrect() {

	return LogicUtils.isAllTrue(driver.waitAndAssertElementNotPresent("CURATOR_SLOT", By.xpath(CURATOR_SLOT)),
		driver.waitAndVerifyElementPresent("EMPTY_SLOT", By.xpath(EMPTY_SLOT)));
    }

    // ************************ GET/SET methods **************************

    @Override
    public String getName() {
	return "Curator's picker page";
    }
}
